package com.bo.day20241016;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

/**
 * 一个二叉树中，两个节点的最近公共祖先节点
 *
 * @Author: gpb
 * @Date: 2024/11/11 17:10
 * @Description:
 */
public class LowestAncestor {
    public static class Node {
        public int value;
        public Node left;
        public Node right;

        public Node (int data) {
            this.value = data;
        }
    }

    /**
     * 信息体
     * 二叉树中找到了A
     * 二叉树中找到了B
     * 二叉树中找到了最近公共祖先节点
     */
    public static class Info {
        public boolean findA;
        public boolean findB;
        public Node ans;

        public Info (boolean findA, boolean findB, Node ans) {
            this.findA = findA;
            this.findB = findB;
            this.ans = ans;
        }
    }

    /**
     * 找到二叉树中两个节点的最近公共祖先节点
     *
     * @param head
     * @param a
     * @param b
     * @return
     */
    public static Node lowestAncestor (Node head, Node a, Node b) {
        return process(head, a, b).ans;
    }

    /**
     * 可能性
     * 1， 与x无关(x不是最低汇集点)
     * 2. 左树有答案了，左树存在最低汇聚到
     * 3. 右树有答案了，右树存在最低汇聚到
     * 4. 不存在a,b情况下，没有汇聚点
     * 5. 与x有关，在左右树找到了a和b,那么就返回x
     * 6. x本身就是a或者是b,在子树发现了a和b，那么就返回x
     * @param x
     * @param a
     * @param b
     * @return
     */
    public static Info process (Node x, Node a, Node b) {
        if (x == null) {
            // 递归到底了,信息体好设置,没有遇到a,b,没有答案
            return new Info(false, false, null);
        }
        Info leftInfo = process(x.left, a, b);
        Info rightInfo = process(x.right, a, b);
        // 如果x==a,或者leftInfo找到了a,rightInfo找到了a,说明二叉树存在a
        boolean findA = x == a || leftInfo.findA || rightInfo.findA;
        // b 同理
        boolean findB = x == b || leftInfo.findB || rightInfo.findB;
        // 公共祖先先设置为null
        Node ans = null;
        // 如果左树存在答案,直接拿过来即可
        if (leftInfo.ans != null) {
            ans = leftInfo.ans;
        } else if (rightInfo.ans != null) {
            // 右树存在答案,直接拿过来即可
            ans = rightInfo.ans;
        } else {
            // 左右树都没有答案,那么就判断x是否是a和b的公共祖先
            if (findA && findB) {
                ans = x;
            }
        }
        return new Info(findA, findB, ans);
    }

    public static Node lowestAncestor1(Node head, Node o1, Node o2) {
        if (head == null) {
            return null;
        }
        // key的父节点是value
        HashMap<Node, Node> parentMap = new HashMap<>();
        parentMap.put(head, null);
        fillParentMap(head, parentMap);
        // 存储o1的所有祖先节点
        HashSet<Node> o1Set = new HashSet<>();
        Node cur = o1;
        o1Set.add(cur);
        // 找到o1的所有祖先节点
        while (parentMap.get(cur) != null) {
            cur = parentMap.get(cur);
            o1Set.add(cur);
        }
        // 找到o2的最近公共祖先节点
        cur = o2;
        // 循环判断o2是否是o1的祖先节点
        while (!o1Set.contains(cur)) {
            cur = parentMap.get(cur);
        }
        return cur;
    }

    public static void fillParentMap(Node head, HashMap<Node, Node> parentMap) {
        if (head.left != null) {
            parentMap.put(head.left, head);
            fillParentMap(head.left, parentMap);
        }
        if (head.right != null) {
            parentMap.put(head.right, head);
            fillParentMap(head.right, parentMap);
        }
    }

    // for test
    public static Node generateRandomBST(int maxLevel, int maxValue) {
        return generate(1, maxLevel, maxValue);
    }

    // for test
    public static Node generate(int level, int maxLevel, int maxValue) {
        if (level > maxLevel || Math.random() < 0.5) {
            return null;
        }
        Node head = new Node((int) (Math.random() * maxValue));
        head.left = generate(level + 1, maxLevel, maxValue);
        head.right = generate(level + 1, maxLevel, maxValue);
        return head;
    }

    // for test
    public static Node pickRandomOne(Node head) {
        if (head == null) {
            return null;
        }
        ArrayList<Node> arr = new ArrayList<>();
        fillPrelist(head, arr);
        int randomIndex = (int) (Math.random() * arr.size());
        return arr.get(randomIndex);
    }

    // for test
    public static void fillPrelist(Node head, ArrayList<Node> arr) {
        if (head == null) {
            return;
        }
        arr.add(head);
        fillPrelist(head.left, arr);
        fillPrelist(head.right, arr);
    }

    public static void main(String[] args) {
        int maxLevel = 4;
        int maxValue = 100;
        int testTimes = 1000000;
        for (int i = 0; i < testTimes; i++) {
            Node head = generateRandomBST(maxLevel, maxValue);
            Node o1 = pickRandomOne(head);
            Node o2 = pickRandomOne(head);
            if (lowestAncestor1(head, o1, o2) != lowestAncestor(head, o1, o2)) {
                System.out.println("Oops!");
            }
        }
        System.out.println("finish!");
    }


}
